package com.corecloud.controller.sys;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.corecloud.entity.SysAreaEntity;
import com.corecloud.entity.SysDeptEntity;
import com.corecloud.entity.SysUserEntity;
import com.corecloud.service.SysAreaService;
import com.corecloud.service.SysDeptService;
import com.corecloud.service.SysUserService;
import com.corecloud.tools.AjaxResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 地域信息管理相关
 */
@Controller
@RequestMapping("/area")
public class AreaController {


	@Autowired
	private SysAreaService sysAreaService;

	@Autowired
	private SysDeptService sysDeptService;

	@Autowired
	private SysUserService sysUserService;


	/**
	 * 地域信息首页
	 * @param principal 登录用户信息
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView index(Principal principal){
		ModelAndView modelAndView = new ModelAndView("area/index");
		return modelAndView;
	}

	/**
	 * 地域信息添加页
	 * @param principal 登录用户信息
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add(Principal principal,String areaId){
		ModelAndView modelAndView = new ModelAndView("area/add");
		if(!StringUtils.isEmpty(areaId)){
			modelAndView.addObject("parentArea",sysAreaService.selectOne(new QueryWrapper<SysAreaEntity>().eq("area_id",areaId)));
			return modelAndView;
		}else{
			modelAndView = new ModelAndView("area/index");
			return modelAndView;
		}
	}


	/**
	 * 保存信息
	 * @param principal
	 * @param info
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public AjaxResultData save(Principal principal, SysAreaEntity info){
		if(StringUtils.isEmpty(info.getAreaParentId())){
			return new AjaxResultData(500,"参数异常,请联系管理员");
		}
		if(StringUtils.isEmpty(info.getAreaId())){
			return new AjaxResultData(500,"请填写地域代码");
		}
		if(StringUtils.isEmpty(info.getAreaName())){
			return new AjaxResultData(500,"请填写地域名称");
		}
		//进行去重验证与插入操作
		try {
			//验证地区代码与名称是否被占用
			SysAreaEntity areaInfo ;
			areaInfo = sysAreaService.selectOne(new QueryWrapper<SysAreaEntity>().eq("area_id",info.getAreaId().trim()));
			if(areaInfo != null && areaInfo.getAreaId() != null){
				return new AjaxResultData(500,"地域代码已被占用");
			}
			areaInfo = sysAreaService.selectOne(new QueryWrapper<SysAreaEntity>().eq("area_name",info.getAreaName().trim()));
			if(areaInfo != null && areaInfo.getAreaName() != null){
				return new AjaxResultData(500,"地域名称已被占用");
			}
			//获取父级地域信息数据,回填至新的信息中
			SysAreaEntity parentArea = sysAreaService.selectOne(new QueryWrapper<SysAreaEntity>().eq("area_id",info.getAreaParentId()));
			areaInfo = new SysAreaEntity();
			areaInfo.setAreaId(info.getAreaId().trim());
			areaInfo.setAreaName(info.getAreaName().trim());
			areaInfo.setAreaParentId(parentArea.getAreaId().trim());
			areaInfo.setAreaTreePath(parentArea.getAreaTreePath().trim() + "/" + info.getAreaId().trim() + "/");
			sysAreaService.insertInfo(areaInfo);
			return new AjaxResultData(200,"操作成功");
		}catch (Exception e){
			return new AjaxResultData(500,"操作失败,请联系管理员");
		}
	}


	/**
	 * 地域信息树形数据
	 * @param principal
	 * @param areaId
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public AjaxResultData delete(Principal principal, String areaId){
		if(StringUtils.isEmpty(areaId)){
			return new AjaxResultData(500,"参数异常,请联系管理员");
		}
		try {
			//需验证本条数据下是否有机构,用户相关信息,如果有则不可删除
			List<SysDeptEntity> list = sysDeptService.selectList(new QueryWrapper<SysDeptEntity>().eq("area_id",areaId));
			if(list != null && list.size() >0){
				return new AjaxResultData(500,"此地域下有机构相关信息,请确认后删除");
			}
			List<SysUserEntity> userList = sysUserService.selectList(new QueryWrapper<SysUserEntity>().eq("area_id",areaId));
			if(userList != null && userList.size() > 0){
				return new AjaxResultData(500, "该地域下有用户挂靠,请确认后删除");
			}
			sysAreaService.deleteInfo(new QueryWrapper<SysAreaEntity>().eq("area_id",areaId));
			return new AjaxResultData(200,"操作成功");
		}catch (Exception e){
			return new AjaxResultData(500,"操作失败,请联系管理员");
		}
	}


	/**
	 * 地域信息树形数据
	 * @param principal
	 * @param pid
	 * @return
	 */
	@RequestMapping("/tree")
	@ResponseBody
	public List<Map> tree(Principal principal, Integer pid){
		//封装页面数据
		QueryWrapper<SysAreaEntity> qw = new QueryWrapper<>();
		if(!StringUtils.isEmpty(pid)){
			qw.eq("area_parent_id",pid);
		}
		List<SysAreaEntity> al = sysAreaService.selectList(qw);
		Map dataMap;
		List<Map> resultList = new ArrayList<>();
		//拼装树形数据
		if(al != null && al.size() > 0){
			for(int i = 0;i<al.size();i++){
				SysAreaEntity sp = al.get(i);
				dataMap = new HashMap();
				dataMap.put("id",sp.getAreaId());
				dataMap.put("name",sp.getAreaName());
				dataMap.put("pid",sp.getAreaParentId() == null ? 0 : sp.getAreaParentId());
				resultList.add(dataMap);
			}
		}
		return resultList;
	}



	/**
	 * 地域列表数据
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public List<SysAreaEntity> list(String name,Integer pid){
		QueryWrapper<SysAreaEntity> qw = new QueryWrapper<>();
		if(pid != null){
			qw.eq("area_parent_id",pid);
		}
		if(!StringUtils.isEmpty(name)){
			qw.like("area_name",name);
		}
		return sysAreaService.selectList(qw);
	}

	/**
	 * 获取所有省份
	 * @return
	 */
	@GetMapping("/provinces")
	@ResponseBody
	public List<SysAreaEntity> getProvinces(){
		List<SysAreaEntity> list = sysAreaService.getProvinces();
		return list;
	}
	/**
	 * 根据省份获得市区
	 * @return
	 */
	@GetMapping("/cities/{provinceId}")
	@ResponseBody
	public List<SysAreaEntity> getCities(@PathVariable("provinceId") String areaParentId){
		List<SysAreaEntity> list = sysAreaService.getCounties(areaParentId);
		return list;
	}

	/**
	 * 根据市区获得县区
	 * @return
	 */
	@GetMapping("/counties/{cityId}")
	@ResponseBody
	public List<SysAreaEntity> getCounties(@PathVariable("cityId") String cityId){
		List<SysAreaEntity> list = sysAreaService.getCounties(cityId);
		return list;
	}



}
